package com.example.city_home_app.Fragment;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.example.city_home_app.Activity.BaseActivity;
import com.example.city_home_app.Bean.Down_Post;
import com.example.city_home_app.Model.ApiModel;
import com.example.city_home_app.R;
import com.example.city_home_app.RecyclerViewAdapter.Adapter_Post;
import com.example.city_home_app.RetrofitListener;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

public class Fragment_FocusPost extends BaseFragment {
    @BindView(R.id.rec_userinfo_post)
    RecyclerView recUserinfoPost;
    @BindView(R.id.refresh_userinfo_post)
    SwipeRefreshLayout refreshUserinfoPost;
    Unbinder unbinder;
    private ApiModel model = new ApiModel();

    private Adapter_Post adapter_post;

    private RetrofitListener<List<Down_Post>> listRetrofitListener = new RetrofitListener<List<Down_Post>>() {
        @Override
        public void onResponse(List<Down_Post> listener) {
            refreshUserinfoPost.setRefreshing(false);
            adapter_post.setList(listener);
            adapter_post.notifyDataSetChanged();
        }

        @Override
        public void onFail(String msg) {

        }
    };


    @Override
    protected void init(View view) {
        unbinder = ButterKnife.bind(this, view);

        recUserinfoPost = (RecyclerView) view.findViewById(R.id.rec_userinfo_post);
        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        recUserinfoPost.setLayoutManager(layoutManager);
        adapter_post = new Adapter_Post(getActivity());
        recUserinfoPost.setAdapter(adapter_post);
        recUserinfoPost.setItemAnimator(new DefaultItemAnimator());

        refreshUserinfoPost = (SwipeRefreshLayout) view.findViewById(R.id.refresh_userinfo_post);
        refreshUserinfoPost.setColorSchemeColors(0xff49afcd);
        refreshUserinfoPost.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refresh();
            }
        });
    }

    private void refresh() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                getActivity().runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        model.getmyfocuspostlist(BaseActivity.getUserinfo().getUserid(),listRetrofitListener);
                    }
                });
            }
        }).start();
    }

    @Override
    protected void initData(Bundle savedInstanceState) {
        model.getmyfocuspostlist(BaseActivity.getUserinfo().getUserid(),listRetrofitListener);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.fragment_userinfo_post;
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }
}
